/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
/**
 * Software License, Version 1.0
 *
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 *
 *
 *Redistribution and use in source and binary forms, with or without
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For
 * further information contact the Community Grids Lab at
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and
 * wherever such third-party acknowledgments normally appear.
 *
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering,
 * shall not be used to endorse or promote products derived from this software
 * without prior written permission from Indiana University.  For written
 * permission, please contact the Advanced Research and Technology Institute
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering,
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 *
 *
 * Indiana University provides no reassurances that the source code provided
 * does not infringe the patent or any other intellectual property rights of
 * any other entity.  Indiana University disclaims any liability to any
 * recipient for claims brought by any other entity based on infringement of
 * intellectual property rights or otherwise.
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS.
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS",
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION
 *GENERATED USING SOFTWARE.
 */
/**
 * <p>Title: EditTemplateView </p>
 * <p>Description: GUI </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Pervasive Tech Lab</p>
 * @modified Beytullah Yildiz
 * @version 1.0
 */

package cgl.narada.gui.admin.reliable.edittable;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import cgl.narada.event.TemplateInfo;
import cgl.narada.event.TemplateProfileAndSynopsisTypes;
import cgl.narada.gui.admin.reliable.DatabaseGuiManager;
import cgl.narada.service.ServiceException;
import cgl.narada.service.reliable.impl.ReliableDeliveryServiceImpl;

public class EditTemplateView
    implements ActionListener {

  private GridBagConstraints gbc;

  private JTextField templateIdField;

  private JTextArea templateArea;
  private JPanel mainPanel, typePanel;
  private JPanel subPanel;
  private JRadioButton stringRadioButton, xmlRadioButton;
  private JRadioButton integerRadioButton, regularExpRadioButton;
  private JRadioButton tagValueExpRadioButton;
  private JButton addButton, removeButton;

  private JLabel sysStatusLabel, templateTypeLabel;
  private JList templateList;
  private ButtonGroup buttonGroup;

  private int templateType;

  public EditTemplateView() {
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());

    sysStatusLabel = new JLabel("System response:");
    mainPanel.add(makeTemplatePanel(), BorderLayout.NORTH);
    mainPanel.add(sysStatusLabel, BorderLayout.SOUTH);
    mainPanel.add(makeSubPanel(), BorderLayout.CENTER);

  }

  public JPanel getPanel() {
    return mainPanel;
  }

  protected JComponent makeSubPanel() {
    JPanel subPanel = new JPanel();
    subPanel.setLayout(new GridBagLayout());
    gbc = new GridBagConstraints();

    // first row
    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    subPanel.add(new JLabel("Template Id :"), gbc);

    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    templateIdField = new JTextField("11111", 30);
    subPanel.add(templateIdField, gbc);

    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    subPanel.add(new JLabel("Template content :"), gbc);

    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;

    templateArea = new JTextArea("Movies/Casablanca", 5, 5);
    JScrollPane messageScroll = new JScrollPane(templateArea,
                                                JScrollPane.
                                                VERTICAL_SCROLLBAR_AS_NEEDED,
                                                JScrollPane.
                                                HORIZONTAL_SCROLLBAR_AS_NEEDED);

    templateType = TemplateProfileAndSynopsisTypes.STRING;
    subPanel.add(messageScroll, gbc);

    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    addButton = new JButton("add template");
    addButton.addActionListener(this);
    subPanel.add(addButton, gbc);

    ////////////////////////////////////////////////

    gbc.insets = new Insets(30, 2, 2, 2);
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    subPanel.add(new JLabel("Select templateId"), gbc);

    gbc.insets = new Insets(30, 2, 2, 2);
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    templateList = new JList(TemplateListModel.getModel());
    templateList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollPane = new JScrollPane(templateList);
    templateList.setFixedCellWidth(40);
    subPanel.add(scrollPane, gbc);

    gbc.insets = new Insets(2, 2, 2, 2);
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.EAST;
    removeButton = new JButton("remove template");
    removeButton.addActionListener(this);
    subPanel.add(removeButton, gbc);

    return subPanel;

  }

  protected JComponent makeTemplatePanel() {

    JPanel panel = new JPanel(false);
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    templateTypeLabel = new JLabel("Template Type : ");

    stringRadioButton = new JRadioButton("String");
    stringRadioButton.setSelected(true);
    xmlRadioButton = new JRadioButton("XML");
    integerRadioButton = new JRadioButton("Integer");
    regularExpRadioButton = new JRadioButton("Regular Expression");
    tagValueExpRadioButton = new JRadioButton("Tag Value Pairs");

    buttonGroup = new ButtonGroup();
    buttonGroup.add(stringRadioButton);
    buttonGroup.add(xmlRadioButton);
    buttonGroup.add(integerRadioButton);
    buttonGroup.add(regularExpRadioButton);
    buttonGroup.add(tagValueExpRadioButton);

//Register a listener for the radio buttons.
    stringRadioButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        templateType = TemplateProfileAndSynopsisTypes.STRING;
        templateIdField.setText("11111");
        templateArea.setText("Movies/Casablanca");
      }
    }
    );

    String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
    xml += "\n<menu>\n <softdrinks>\n  <brand>Minute Maid</brand>";
    xml += "\n  <fruit>Apple</fruit>\n  <source>Brazil</source>";
    xml += "\n  <company>Coca Cola</company>\n  <price>2.90</price>";
    xml += "\n  <year>2004</year>\n </softdrinks>\n</menu>";
    final String xml_ = xml;

    xmlRadioButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        templateType = TemplateProfileAndSynopsisTypes.XML;
        templateIdField.setText("22222");
        templateArea.setText(xml_);
      }
    }
    );

    integerRadioButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        templateType = TemplateProfileAndSynopsisTypes.INTEGER;
        templateIdField.setText("33333");
        templateArea.setText("123456789");
      }
    }
    );

    regularExpRadioButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        templateType = TemplateProfileAndSynopsisTypes.REGULAR_EXPRESSIONS;
        templateIdField.setText("44444");
        templateArea.setText("The quick brown fox jumps upon the lazy dog");
      }
    }
    );

    tagValueExpRadioButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        templateType = TemplateProfileAndSynopsisTypes.TAG_VALUE_PAIRS;
        templateIdField.setText("55555");
        templateArea.setText("Sport=NBA, Team=Bulls, Player=Jordan");
      }
    }
    );

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(templateTypeLabel, c);

    c.gridx = 2;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(stringRadioButton, c);

    c.gridx = 4;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(xmlRadioButton, c);

    c.gridx = 6;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(integerRadioButton, c);

    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(regularExpRadioButton, c);

    c.gridx = 4;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    panel.add(tagValueExpRadioButton, c);

    return panel;
  }

  public void actionPerformed(ActionEvent ae) {
    try {

      if (ae.getSource() == addButton) {
        ReliableDeliveryServiceImpl rds = DatabaseGuiManager.
            getReliableDeliverService();
        int templateId = Integer.parseInt(templateIdField.getText());

        String textarea = templateArea.getText().trim();
        //  System.out.println("textarea " +textarea);
        TemplateInfo templateInfo =
            rds.createTemplateInfo(templateId,
                                   templateType,
                                   textarea);
        rds.addTemplateManagement(templateInfo);
        TemplateListModel.addTemplateId(templateId);
      }
      else if (ae.getSource() == removeButton) {
        int templatePos = templateList.getSelectedIndex();
        TemplateListModel.removeTemplateId(templatePos);
        System.out.println("take remove actions");
      }
//      Handle RadioButtons
//      else if(false){
//
//      }

    }
    catch (NumberFormatException ex) {
      ex.printStackTrace();
    }
    catch (ServiceException ex) {
      ex.printStackTrace();
    }
  }
}